/*     */ package com.zimbra.cs.service.util;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ParseMailboxID
/*     */ {
/*  42 */   private String hostname = null;
/*  43 */   private int mailboxId = 0;
/*  44 */   private boolean isLocal = false;
/*  45 */   private boolean allMailboxIds = false;
/*  46 */   private boolean allServers = false;
/*     */   private String initialString;
/*     */   private Account account;
/*     */   
/*     */   private ParseMailboxID() {}
/*     */   
/*     */   private ParseMailboxID(Account acct, String idStr) throws ServiceException
/*     */   {
/*  54 */     init(acct, idStr);
/*     */   }
/*     */   
/*     */   public static ParseMailboxID parse(String idStr)
/*     */     throws ServiceException
/*     */   {
/*     */     try
/*     */     {
/*  62 */       return new ParseMailboxID(idStr);
/*     */     } catch (IllegalArgumentException e) {
/*  64 */       throw ServiceException.FAILURE("Error parsing MailboxID specifier: " + idStr, e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public static ParseMailboxID serverAll(String serverID)
/*     */     throws ServiceException
/*     */   {
/*  72 */     return parse("/" + serverID + "/*");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public String getString()
/*     */   {
/*  80 */     return this.initialString;
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/*  85 */     return getString();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public boolean isLocal()
/*     */   {
/*  92 */     return this.isLocal;
/*     */   }
/*     */   
/*     */   public Account getAccount() {
/*  96 */     return this.account;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public String getServer()
/*     */   {
/* 103 */     return this.hostname;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public int getMailboxId()
/*     */   {
/* 111 */     return this.mailboxId;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public boolean isAllMailboxIds()
/*     */   {
/* 118 */     return this.allMailboxIds;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public boolean isAllServers()
/*     */   {
/* 125 */     return this.allServers;
/*     */   }
/*     */   
/*     */   private ParseMailboxID(Account account) throws ServiceException, IllegalArgumentException {
/* 129 */     init(account, null);
/*     */   }
/*     */   
/*     */   private void init(Account account, String idStr) throws ServiceException, IllegalArgumentException {
/* 133 */     this.account = account;
/* 134 */     this.hostname = account.getAttr("zimbraMailHost");
/* 135 */     this.initialString = (idStr == null ? account.getId() : idStr);
/* 136 */     if (Provisioning.onLocalServer(account)) {
/* 137 */       this.isLocal = true;
/*     */     }
/*     */   }
/*     */   
/*     */   private ParseMailboxID(String idStr) throws ServiceException, IllegalArgumentException {
/* 142 */     this.initialString = idStr;
/* 143 */     Account acct = null;
/* 144 */     if (idStr.indexOf('@') >= 0) {
/* 145 */       acct = Provisioning.getInstance().get(Key.AccountBy.name, idStr);
/* 146 */       if (acct == null) {
/* 147 */         throw AccountServiceException.NO_SUCH_ACCOUNT(idStr);
/*     */       }
/* 149 */       init(acct, idStr);
/* 150 */     } else if (idStr.indexOf('/') >= 0)
/*     */     {
/* 152 */       String[] substrs = idStr.split("/");
/*     */       
/* 154 */       if ((substrs.length != 3) || (substrs[0].length() != 0)) {
/* 155 */         if (substrs.length == 2) {
/* 156 */           throw new IllegalArgumentException("Invalid mailboxID (missing initial '/' ?): " + idStr);
/*     */         }
/* 158 */         throw new IllegalArgumentException("Invalid MailboxID: " + idStr);
/*     */       }
/*     */       
/*     */ 
/* 162 */       this.hostname = substrs[1];
/* 163 */       if (this.hostname.equals("*")) {
/* 164 */         this.allServers = true;
/*     */       }
/*     */       
/* 167 */       if (substrs[2].startsWith("*")) {
/* 168 */         this.allMailboxIds = true;
/*     */       } else {
/* 170 */         if (this.allServers) {
/* 171 */           throw new IllegalArgumentException("Invalid mailboxID (\"*/number is not allowed): " + idStr);
/*     */         }
/* 173 */         this.mailboxId = Integer.parseInt(substrs[2]);
/*     */       }
/* 175 */       this.isLocal = this.hostname.equals(Provisioning.getInstance().getLocalServer().getServiceHostname());
/* 176 */     } else if (idStr.indexOf('-') >= 0) {
/* 177 */       acct = Provisioning.getInstance().get(Key.AccountBy.id, idStr);
/* 178 */       if (acct == null) {
/* 179 */         throw AccountServiceException.NO_SUCH_ACCOUNT(idStr);
/*     */       }
/* 181 */       init(acct, idStr);
/*     */     }
/* 183 */     else if (idStr.equals("*")) {
/* 184 */       this.hostname = "*";
/* 185 */       this.allMailboxIds = true;
/* 186 */       this.allServers = true;
/*     */     } else {
/* 188 */       this.mailboxId = Integer.parseInt(idStr);
/* 189 */       this.isLocal = true;
/*     */     }
/*     */   }
/*     */   
/*     */   private void initAllMailboxes()
/*     */   {
/* 195 */     this.hostname = "*";
/* 196 */     this.allMailboxIds = true;
/* 197 */     this.allServers = true;
/*     */   }
/*     */   
/*     */   public static ParseMailboxID allMailboxes() {
/* 201 */     ParseMailboxID pmid = new ParseMailboxID();
/* 202 */     pmid.initAllMailboxes();
/* 203 */     return pmid;
/*     */   }
/*     */   
/*     */   public static ParseMailboxID byAccount(Account acct) throws ServiceException {
/* 207 */     return new ParseMailboxID(acct, null);
/*     */   }
/*     */   
/*     */   public static ParseMailboxID byEmailAddress(String idStr) throws ServiceException {
/* 211 */     Account acct = Provisioning.getInstance().get(Key.AccountBy.name, idStr);
/* 212 */     if (acct == null) {
/* 213 */       throw AccountServiceException.NO_SUCH_ACCOUNT(idStr);
/*     */     }
/* 215 */     return new ParseMailboxID(acct, idStr);
/*     */   }
/*     */   
/*     */   public static ParseMailboxID byAccountId(String idStr) throws ServiceException {
/* 219 */     Account acct = Provisioning.getInstance().get(Key.AccountBy.id, idStr);
/* 220 */     if (acct == null) {
/* 221 */       throw AccountServiceException.NO_SUCH_ACCOUNT(idStr);
/*     */     }
/* 223 */     return new ParseMailboxID(acct, idStr);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/util/ParseMailboxID.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */